package com.googlecode.allinclusive.fw.coderesource;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.googlecode.allinclusive.fw.database.dao.QueryDAO;
import com.googlecode.allinclusive.fw.springframework.utils.SpringUtils;

public class DefaultCodeManager extends AbstractCodeManager {
	
	/** log instance */
	private static final Log log = LogFactory.getLog(DefaultCodeManager.class);
	
	/**
	 * load resource
	 */
	public void load() {
		log.info("DefaultCodeManager#load is started...");
		QueryDAO queryDAO = SpringUtils.getBean(QueryDAO.class);
		List<CodeValue> codeValueList = queryDAO.executeForObjectList("CODE.selectAllDatas", null);
		Map<String, List<CodeValue>> cacheMap = new ConcurrentHashMap<String, List<CodeValue>>();
		for (CodeValue codeValue : codeValueList) {
			List<CodeValue> list = cacheMap.get(codeValue.getCodeId());
			if (list == null) {
				list = new ArrayList<CodeValue>();
				cacheMap.put(codeValue.getCodeId(), list);
			}
			list.add(codeValue);
		}
		
		this.cacheMap = cacheMap;
	}
}
